package com.yzh;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;
import java.util.List;

/**
 * @author 于振晗
 * @title: TestDom4j
 * @description: TODO Java_获取XMl数据
 * @date 2020年09月08日
 * @path com.yzh
 */
public class TestDom4j01 {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File("Java_Xml/test.xml");
        Document doc = reader.read(file);

        Element rootElement = doc.getRootElement();
        Iterator<Element> it = rootElement.elementIterator("product");

        while (it.hasNext()) {
            Element next = it.next();
            Attribute id = next.attribute("id");
            System.out.println(id.getName()+"---->"+id.getValue());

            List<Element> elements = next.elements();
            for (Element element : elements) {
                System.out.println(element.getName() + "--->" + element.getText());
            }
        }
    }
}
